package com.tang.sharding.enums;

import com.tang.sharding.core.ShardTableRuleConfig;
import lombok.Getter;

/**
 * @author Tang
 * @classname ShardingCycle
 * @description [ 分片周期 ]
 * @date 2021/7/31 10:54
 */
public enum ShardingCycle {

    /**
     * 每天晚上11点
     */
    DAY("0 0 23 1/1 * ? ", "%s.%s_${%s..%s}_${1..12}_${1..31}"),
//    DAY("0/5 * * * * ?", "%s.%s_${%s..%s}_${1..12}_${1..31}"),
    /**
     * 每周3,23点
     */
    WEEK("0 0 23 ? * WED", "%s.%s_${[%s]}"),
    /**
     * 每个月的15号,25,30号的23点
     */
    MONTH("0 0 23 15,25,30 * ?", "%s.%s_${%s..%s}_${1..12}"),
    /**
     * 每个月底的23点
     */
    YEAR("0 0 23 31 1/1 ? ", "%s.%s_${%s..%s}"),

    /**
     * 原生
     */
    NON("","");

    @Getter
    private String cron;

    private String actualDataNodes;

    private final String SHARDING_START_YEAR = "2021";
    private final String SHARDING_END_YEAR = "2031";

    ShardingCycle(String cron, String actualDataNodes) {
        this.cron = cron;
        this.actualDataNodes = actualDataNodes;
    }

    /**
     * 节点表达式
     *
     * @param dataSourceName
     * @param tableName
     * @return
     */
    public String getActualDataNodes(String dataSourceName, String tableName) {
        if (this == WEEK) {
            return String.format(actualDataNodes, dataSourceName, tableName, ShardTableRuleConfig.getActualDataNodes(SHARDING_END_YEAR));
        }
        return String.format(actualDataNodes, dataSourceName, tableName, SHARDING_START_YEAR, SHARDING_END_YEAR);
    }
}
